<?php

namespace app\api\model;

use think\Model;

class UserWeb extends Model
{

    protected $pk = 'user_id';

    public static function getUserInfo($userName, $userMobile)
    {
        return self::where(['user_name' => $userName, 'user_mobile' => $userMobile])->find();
    }

    public static function updateUserInfo($userId, $update)
    {
        return self::where('user_id', $userId)->update($update);
    }

    public static function dealReturnData($userInfo, $refreshToken = true)
    {
        $data = [];
        $data['user_id'] = $userInfo['user_id'];
        $data['user_name'] = $userInfo['user_name'];
        $data['user_avatar'] = dealImage($userInfo['user_avatar']);
        $data['user_sex'] = $userInfo['user_sex'];
        $data['user_birth'] = $userInfo['user_birth'] ? date('Y-m-d', $userInfo['user_birth']) : '';
        $data['user_profile'] = $userInfo['user_profile'];
        $data['user_mobile'] = $userInfo['user_mobile'];
        $token['user_id'] = $userInfo['user_id'];
        if ($refreshToken) {
            $token['login_time'] = time();
            $token['expire_time'] = time() + 86400 * 30;
            $data['user_token'] = aesEncrypt(json_encode($token));
        }
        return $data;
    }

    public static function getInfoById($userId)
    {
        return self::find($userId);
    }

    public static function updateLoginTime($userId, $token)
    {
        return self::where('user_id', $userId)->update(['user_token' => $token, 'login_time' => time()]);
    }
}